﻿using NoteHelper.IBusiness;
using NoteHelper.Utils;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Unity;

namespace NoteHelper
{
    public partial class SettingForm : Form
    {
        private string _dbFile;

        public SettingForm()
        {
            InitializeComponent();

            var container = UnityHelper.GetContainer();
            var mapper = container.Resolve<IDbMapper>();
            _dbFile = mapper.GetDbFile();
        }

        public bool DbFileExists()
        {
            return File.Exists(_dbFile);
        }

        private string DbFile
        {
            get { return grid[1, 1].Value.ToString().Trim(); }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            grid.Redim(5, 2);

            SourceGrid.Cells.Views.Cell titleModel = new SourceGrid.Cells.Views.Cell();
            titleModel.BackColor = Color.SteelBlue;
            titleModel.ForeColor = Color.White;
            titleModel.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            SourceGrid.Cells.Views.Cell captionModel = new SourceGrid.Cells.Views.Cell();
            captionModel.BackColor = grid.BackColor;

            int currentRow = 0;

            #region 数据库设置
            grid[currentRow, 0] = new SourceGrid.Cells.Cell("数据库设置");
            grid[currentRow, 0].View = titleModel;
            grid[currentRow, 0].ColumnSpan = 2;
            currentRow++;

            grid[currentRow, 0] = new SourceGrid.Cells.Cell("数据库");
            grid[currentRow, 0].View = captionModel;
            grid[currentRow, 1] = new SourceGrid.Cells.Cell(_dbFile, typeof(string));

            currentRow++;
            
            grid[currentRow, 0] = new SourceGrid.Cells.Cell("用户名");
            grid[currentRow, 0].View = captionModel;
            grid[currentRow, 1] = new SourceGrid.Cells.Cell("sa", typeof(string));

            currentRow++;

            grid[currentRow, 0] = new SourceGrid.Cells.Cell("密码");
            grid[currentRow, 0].View = captionModel;
            grid[currentRow, 1] = new SourceGrid.Cells.Cell("", typeof(string));

            currentRow++;
            #endregion

            #region 按钮

            grid[currentRow, 0] = new SourceGrid.Cells.Button("初始化数据库");
            SourceGrid.Cells.Controllers.Button dbInitButton = new SourceGrid.Cells.Controllers.Button();
            dbInitButton.Executed += new EventHandler(DbInitButton_Click);
            grid[currentRow, 0].Controller.AddController(dbInitButton);

            grid[currentRow, 1] = new SourceGrid.Cells.Button("保存设置");
            SourceGrid.Cells.Controllers.Button acceptChangesButton = new SourceGrid.Cells.Controllers.Button();
            acceptChangesButton.Executed += new EventHandler(AcceptChangesButton_Click);
            grid[currentRow, 1].Controller.AddController(acceptChangesButton);

            #endregion

            //Stretch only the last column
            grid.Columns[0].AutoSizeMode = SourceGrid.AutoSizeMode.MinimumSize | SourceGrid.AutoSizeMode.Default;
            grid.Columns[1].AutoSizeMode = SourceGrid.AutoSizeMode.MinimumSize | SourceGrid.AutoSizeMode.Default;
            grid.AutoSizeCells();
            grid.AutoStretchColumnsToFitWidth = true;
            grid.Columns.StretchToFit();
        }

        private void DbInitButton_Click(object sender, EventArgs e)
        {
            var container = UnityHelper.GetContainer();
            var mapper = container.Resolve<IBusiness.IDbMapper>();
            mapper.SetDbFile(DbFile);

            var manager = container.Resolve<IArticleAttributeManager>();
            manager.InitDb();
            Close();
        }

        private void AcceptChangesButton_Click(object sender, EventArgs e)
        {
            var container = UnityHelper.GetContainer();
            var mapper = container.Resolve<IBusiness.IDbMapper>();
            mapper.SetDbFile(DbFile);
            MessageBox.Show("配置会在软件下次启动时生效！", "提示");
        }
    }
}
